unit AI_Schagron;

{
  An AI wich just buys inventions
}

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, basicAI, basicio_City, basicio_street, basicio_field,
  basicio_resources, systemmanager,math;

type

  { TAI_Schagroon }
  TAI_Schagron = class (TBasicAI)
  protected
    function ReturnCity: TCityPosition; override;
    function ReturnStreet: TStreetPosition; override;
    function ReturnThiefField: TFieldPosition; override;
    function ReturnThiefCity: TCityPosition; override;
    function ReturnThrowRes(AAmount: integer): TResources; override;
    function ReadyToAct: boolean; override;
    function WantToTrade: boolean; override;
    procedure HandleBuilding; override;
    function ReturnExtendCity: TCityPosition; override;
  end;

implementation


function TAI_Schagron.ReturnCity: TCityPosition;
begin
  result.y := random(3)+2;
  case result.y of
    2: result.x := random(4)+4;
    3: result.x := random(6)+3;
    4: result.x := random(6)+3;
    5: result.x := random(4)+4;
  end;
end;

function TAI_Schagron.ReturnStreet: TStreetPosition;
var
  i,j: integer;
  possiblePos: array[0..53] of TCityPosition;
begin
  for i := 0 to 53 do
  begin
    possiblePos[i].x := 0;
  end;
  for i := 0 to GlobalSystemmanager.io.data.map.CityCount-1 do
  begin
    if not(assigned(GlobalSystemmanager.io.data.map.CityByID[i])) then
    begin
      continue;
    end;
    if (GlobalSystemmanager.io.data.map.CityByID[i].Owner
          = GlobalSystemmanager.io.data.Player[AIPlayerID]) then
    begin
      possiblePos[i] := GlobalSystemmanager.io.data.map.CityByID[i].Position;
    end;
  end;
  j := 0;
  while (possiblePos[j].x = 0) do  // gets any of its own cities
  begin
    j := random(53);
  end;
  result[0] := possiblePos[j];
  case random(3) of
    0:
      begin
       result[1].x := result[0].x;
       result[1].y := result[0].y+1;
      end;
    1:
      begin
       result[1].x := result[0].x;
       result[1].y := result[0].y-1;
      end;
    2:
      begin
       result[1].x := result[0].x+1;
       result[1].y := result[0].y;
      end;
    3:
      begin
       result[1].x := result[0].x-1;
       result[1].y := result[0].y;
      end;
  end;
end;

function TAI_Schagron.ReturnThiefField: TFieldPosition;
begin
  result.x:= random(5)+1;
  case result.x of
    1,6:
      begin
        result.y := random(6)+3;
      end;
    2,5:
      begin
        result.y := random(8)+2;
      end;
    3,4:
      begin
        result.y := random(10)+1;
      end;
  end;
end;

function TAI_Schagron.ReturnThiefCity: TCityPosition;
var
  i,j: integer;
  possiblePos: array[0..53] of TCityPosition;
begin
  for i := 0 to 53 do
  begin
    possiblePos[i].x := 0;
  end;
  for i := 0 to GlobalSystemmanager.io.data.map.CityCount-1 do
  begin
    if not(assigned(GlobalSystemmanager.io.data.map.CityByID[i])) then
    begin
      continue;
    end;
    if not(GlobalSystemmanager.io.data.map.CityByID[i].Owner
          = GlobalSystemmanager.io.data.Player[AIPlayerID]) then
    begin
      possiblePos[i] := GlobalSystemmanager.io.data.map.CityByID[i].Position;
    end;
  end;
  j := 0;
  while (possiblePos[j].x = 0) do  // gets any of the other's cities
  begin
    j := random(53);
  end;
  result := possiblePos[j];
end;

function TAI_Schagron.ReturnThrowRes(AAmount: integer): TResources;
var
  throw: integer;
begin
  result.Dirt:= GlobalSystemmanager.io.data.Player[AIPlayerID].Resources.Dirt;
  result.Wood:= GlobalSystemmanager.io.data.Player[AIPlayerID].Resources.Wood;
  if (GlobalSystemmanager.io.data.Player[AIPlayerID].Resources.Dirt
      +GlobalSystemmanager.io.data.Player[AIPlayerID].Resources.Wood < AAmount) then
  begin
    // ToDo: errechne differenz und for (i to diff) do folgendes
    throw := math.max(math.max(GlobalSystemmanager.io.data.Player[AIPlayerID].Resources.Ore,
                             GlobalSystemmanager.io.data.Player[AIPlayerID].Resources.Wheat),
                    GlobalSystemmanager.io.data.Player[AIPlayerID].Resources.Sheeps);
    if (GlobalSystemmanager.io.data.Player[AIPlayerID].Resources.Ore = throw) then
    begin
      result.Ore:= 1;
    end;
    if (GlobalSystemmanager.io.data.Player[AIPlayerID].Resources.Wheat = throw) then
    begin
      result.Wheat:= 1;
    end;
    if (GlobalSystemmanager.io.data.Player[AIPlayerID].Resources.Sheeps = throw) then
    begin
      result.Sheeps:= 1;
    end;
  end;
end;

function TAI_Schagron.ReadyToAct: boolean;
begin
  Result:=inherited ReadyToAct;
end;

function TAI_Schagron.WantToTrade: boolean;
begin
  Result:=inherited WantToTrade;
end;

procedure TAI_Schagron.HandleBuilding;
begin
  HandleExtendCity;
  HandleInventions;
end;

function TAI_Schagron.ReturnExtendCity: TCityPosition;
var
  i,j: integer;
  possiblePos: array[0..53] of TCityPosition;
begin
  for i := 0 to 53 do
  begin
    possiblePos[i].x := 0;
  end;
  for i := 0 to GlobalSystemmanager.io.data.map.CityCount-1 do
  begin
    if not(assigned(GlobalSystemmanager.io.data.map.CityByID[i])) then
    begin
      continue;
    end;
    if ((GlobalSystemmanager.io.data.map.CityByID[i].Owner
          = GlobalSystemmanager.io.data.Player[AIPlayerID])
        and (not(GlobalSystemmanager.io.data.map.CityByID[i].isExtended))) then
    begin
      possiblePos[i] := GlobalSystemmanager.io.data.map.CityByID[i].Position;
    end;
  end;
  j := 0;
  while (possiblePos[j].x = 0) do  // gets any of its own cities
  begin
    j := random(53);
  end;
  result := possiblePos[j];
end;

{ TAI_Schagron }

initialization
  GlobalSystemManager.RegisterAISystem(TAI_Schagron);

end.


